搜索 K
Appearance
博客正在加载中...
Appearance
接下来,我们就正式讲解 web 开发的场景
该场景用的多,知识点多、细,底层原理复杂,因此我们讲的也多:
先讲怎么用,在讲原理
相关官方文档:
首先,SpringBoot 是框架的框架,底层仍然用的是 SpringMVC,并且配置好了 SpringMVC;
那么具体配置了什么呢?我们来看看:
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
The auto-configuration adds the following features on top of Spring’s defaults:
- Inclusion of
ContentNegotiatingViewResolverandBeanNameViewResolverbeans.- Support for serving static resources, including support for WebJars (covered later in this document)).
- Automatic registration of
Converter,GenericConverter, andFormatterbeans.- Support for
HttpMessageConverters(covered later in this document).- Automatic registration of
MessageCodesResolver(covered later in this document).- Static
index.htmlsupport.- Custom
Faviconsupport (covered later in this document).- Automatic use of a
ConfigurableWebBindingInitializerbean (covered later in this document). 大意:
SpringBoot 给 SpringMVC,提供了自动配置,并且大多数场景下都能运作顺利。
自动配置添加了如下默认配置:
Converter,GenericConverter,FormatterHttpMessageConverters (后来我们配合内容协商理解原理)MessageCodesResolver (用于国际化,一般用的少)Favicon(图标)ConfigurableWebBindingInitializer,(DataBinder 负责将请求数据绑定到 JavaBean 上) 也支持定制化:If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own
@Configurationclass of typeWebMvcConfigurerbut without@EnableWebMvc.If you want to provide custom instances of
RequestMappingHandlerMapping,RequestMappingHandlerAdapter, orExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of typeWebMvcRegistrationsand use it to provide custom instances of those components.If you want to take complete control of Spring MVC, you can add your own
@Configurationannotated with@EnableWebMvc, or alternatively add your own@Configuration-annotatedDelegatingWebMvcConfigurationas described in the Javadoc of@EnableWebMvc.
大意:
不用@EnableWebMvc 注解,使用 @Configuration + WebMvcConfigurer 自定义规则(比如拦截器,格式化器,视图控制器)
声明 WebMvcRegistrations 改变默认底层组件
使用 ** ** @EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC